/*
 * @author: M-A. Taupenot
 * @version 1.0.0
 * date: 21/05/2012
 */
package tests;

import static org.junit.Assert.*;

import org.junit.*;

import java.util.*;

import subscribersManagement.Subscriber;
import loansManagement.Loan;
import booksManagement.Book;
import booksManagement.DVD;
import booksManagement.Copy;
import booksManagement.Media;
import exceptions.*;

/**
 * @author        user
 */
public class DVDTest
{
	/**
   * @uml.property  name="s"
   * @uml.associationEnd  
   */
	private Subscriber s;
	
	private ArrayList<Media> medias = new ArrayList<Media>();
	/**
   * @uml.property  name="b"
   * @uml.associationEnd  
   */
	private Book b;
	/**
   * @uml.property  name="d"
   * @uml.associationEnd  
   */
	private DVD d;
	/**
   * @uml.property  name="c1"
   * @uml.associationEnd  
   */
	private Copy c1;
	/**
   * @uml.property  name="c2"
   * @uml.associationEnd  
   */
	private Copy c2;
	private ArrayList<String> authors;
	
	@Before
	public void beforeTests() throws BadParametersException {
		s = new Subscriber("Mayte", "Segarra", new GregorianCalendar());
		
		authors = new ArrayList<String>();
		authors.add("Corral");
		b = new Book("Noche", authors, new GregorianCalendar(), "026765415");
		d = new DVD("Nemesis",new GregorianCalendar(),"1237860", 2);
		d.addCopy();
		c1 = b.addCopy();
		c2 = d.getCopies().get(2);
		
	}
	
	@Test
	public void testEqualsDvd() throws BadParametersException {
		DVD d2 = new DVD("Nemesis",new GregorianCalendar(),"1237860");
		assertTrue(d.equals(d2));
		
		d2 = new DVD("Nemesis", new GregorianCalendar(),"166476114");
		assertFalse(d.equals(d2));
		
		d2 = new DVD("The World",new GregorianCalendar(),"1237860");
		assertTrue(d.equals(d2));
		
	}

	@Test
	public void testManyCopiesDVD() throws BadParametersException {
		DVD d2 = new DVD("Nemesis",new GregorianCalendar(),"1237860",2);
		assertTrue(d2.getCopies().size()==2);
		System.out.println(d2.getCopies().size());
	}
	
	@Test
	public void testOKDVD() throws BadParametersException {
		System.out.println(medias);
		DVD d2 = new DVD("Nemesis", new GregorianCalendar(), "020178515");
		medias.add(d2);
		System.out.println(medias);
	}
	
	@Test(expected = BadParametersException.class)
	public void testNullDVD() throws BadParametersException{
		new DVD("Noche", new GregorianCalendar(), null);
		new DVD("Nemesis", null, "0546456687");
		new DVD(null, new GregorianCalendar(), "446673");
	}
	
	@Test
	public void testLendDVD() throws BadParametersException, 
	TooManyLoansException, LentBookException, ReservedCopyException{
		System.out.println("nombre d'emprunts : " + s.getCurrentLoans().size());
		Loan l = new Loan(c2,s);
		assertTrue(c2.getCopyLoan() != null);
		System.out.println("abonné : " + l.getLender());
		System.out.println("nombre d'emprunts : " + l.getLender().getCurrentLoans().size());
	}
	
	@Test(expected = LentBookException.class)
	public void testLendLentDVD() throws BadParametersException, 
	TooManyLoansException, LentBookException, ReservedCopyException{
		new Loan(c2,s);
		//s = new Subscriber("Mayte", "Seg", new GregorianCalendar());
		
		new Loan(c2,s);
	}
	
	@Test
	public void testReturnDVD() throws BadParametersException, 
	TooManyLoansException, LentBookException, ReservedCopyException{
		new Loan(c2,s);
		
		c2.returnCopy();
		assertTrue(c2.getCopyLoan() == null);
	}
}
